var express = require('express');
var router = express.Router();
var mongo = require("./db");
var mongodb = require("mongodb");
var ObjectId = mongodb.ObjectId;
/* GET users listing. */
router.get('/', function (req, res, next) {
  res.send('respond with a resource');
});
// 添加垃圾接口
router.post("/add", function (req, res) {
  var obj = {
    id: +new Date(),
    lename: req.body.lename,
    sel: req.body.sel,
    date: new Date().toLocaleString()
  }
  mongo("insert", "leselist", obj, function () {
    res.send({ success: "添加成功" })
  })
})
// 查询接口
router.post("/getlist", function (req, res) {
  var obj = {}
  if (req.body.lename) {
    obj.lename = req.body.lename;
  }
  if (req.body.sel) {
    obj.sel = req.body.sel
  }
  mongo("find", "leselist", obj, function (data) {
    res.send(data)
  })
})
// 删除接口
router.post("/del", function (req, res) {
  mongo("del", "leselist", { _id: ObjectId(req.body._id) }, function (data) {
    res.send({ success: "删除成功" })
  })
})
// 修改接口
router.post("/update", function (req, res) {
  mongo("update", "leselist", [{ _id: ObjectId(req.body._id) }, { lename: req.body.lename, sel: req.body.sel }], function () {
    res.send({ success: "修改成功" })
  })
})



module.exports = router;
